package thread;


import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ThreadVolatile1 {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("主线程开始====");
        new Thread(new Run(),"thread2").start();
        // 休眠保证读取的flag的顺序
        Thread.sleep(1000);
        Parameter.flag = false;
        System.out.println("主线程关闭====");
    }


}
@Slf4j
class Run implements Runnable{
    @Override
    public void run() {
        while(Parameter.flag) {
            //什么都没做
            // 不要用System.out.println(111);，会加锁自动同步
//            log.info("111");
        }
    }
}
class Parameter {
    public static volatile boolean flag = true;
}